import axios from 'axios';
import md5 from 'js-md5';

let URL = 'https://www.77.top/';
// let URL = 'http://beta.www.77.top/';
let domain = 'http://77.top';
// let domain = 'http://beta.um.77.top';

if (process.client) {
  URL = '/aa';
}

// 定义 getCookie 函数
function getCookie(name) {
  if (process.client) {
    const value = `; ${document.cookie}`;
    const parts = value.split(`; ${name}=`);
    if (parts.length === 2) return parts.pop().split(';').shift();
  }
  return '';
}

// 使用 getCookie 函数
let uid = getCookie('loginnew');
let t = Date.now();
let sign = `${uid}${t}fsd213ewdsadqwe2121213edsad`;
let textToHash = md5(sign);

export default ({ app }, inject) => {

  const instance = axios.create({
    baseURL: URL,
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded',
      'Accept': '*/*',
    }
  });

  // 添加请求拦截器
  instance.interceptors.request.use(config => {
    return config;
  }, error => {
    return Promise.reject(error);
  });

  // 添加响应拦截器
  instance.interceptors.response.use(response => {
    if(response.data.message=='错误01 请重新登录'){
      window.location.href = `${domain}/?ct=wap&ac=login`;
    }else{
      return response;
    } 
    // return response;
  }, error => {
    return Promise.reject(error);
  });

  // 将封装后的Axios实例注入到Vue实例中
  inject('axios', instance);
};
